

//
//  WelcomeViewController.m
//  OCMicroBlog
//
//  Created by 002 on 15/11/6.
//  Copyright © 2015年 002. All rights reserved.
//

#import "WelcomeViewController.h"
#import "UIImageView+Extention.h"
#import "UILabel+Extention.h"
#import "masonry.h"
#import "UIView+Extention.h"
#import "UIImageView+WebCache.h"
#import "UserAccountViewModel.h"

@interface WelcomeViewController ()

// 背景图像
@property(nonatomic,strong) UIImageView *backImageView;

// 头像图像
@property(nonatomic,strong) UIImageView *iconView;

// 欢迎标签
@property(nonatomic,strong) UILabel *welcomeLabel;

@end

@implementation WelcomeViewController

#pragma mark - 视图生命周期

// 在此设置界面动画
- (void)viewDidAppear:(BOOL)animated {

    [super viewWillAppear:animated];
    
    // 更新图像位置
    [self.iconView mas_updateConstraints:^(MASConstraintMaker *make) {
        make.bottom.equalTo(self.view).offset(-self.view.height + 200);
    }];
    
    // 执行动画
    [UIView animateWithDuration:1
                          delay:0
         usingSpringWithDamping:0.8
          initialSpringVelocity:5
                        options:0 animations:^{
        
        [self.view layoutIfNeeded];
    } completion:^(BOOL finished) {
        
        [UIView animateWithDuration:0.8 animations:^{
            
            self.welcomeLabel.alpha = 1;
        } completion:^(BOOL finished) { // 动画执行完毕后发送通知进入主界面
            
            // 发送通知
            [[NSNotificationCenter defaultCenter] postNotificationName:WBSwitchRootViewControllerNotification object:nil];
        }];
    }];
}

- (void)viewDidLoad {
    [super viewDidLoad];

    // view的大小会自动适应屏幕的大小，backImageView也会自动撑大
    self.view = self.backImageView;
    
    // 设置头像
    [self.iconView sd_setImageWithURL:[UserAccountViewModel sharedAccountViewModel].avatarUrl placeholderImage:[UIImage imageNamed:@"avatar_default_big"]];
    NSLog(@"%@",[UserAccountViewModel sharedAccountViewModel].avatarUrl);
    // 设置界面
    [self setupUI];

}

#pragma mark - 设置界面
- (void)setupUI {
    
    // 1. 添加控件
    [self.view addSubview:self.iconView];
    [self.view addSubview:self.welcomeLabel];
    
    // 先隐藏标签
    self.welcomeLabel.alpha = 0;
    
    // 2. 布局
    // 1) 头像
    [self.iconView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.view.mas_bottom).offset(-200);
        make.width.equalTo(@90);
        make.height.equalTo(@90);        
    }];
    // 2) 欢迎标签
    [self.welcomeLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(self.view.mas_centerX);
        make.bottom.equalTo(self.iconView.mas_bottom).offset(26);
    }];
}

#pragma mark - 懒加载控件
- (UIImageView *)backImageView {
    if (!_backImageView) {
        _backImageView = [UIImageView initWithImageName:@"ad_background"];
    }
    return _backImageView;
}

- (UIImageView *)iconView {
    if (!_iconView) {
        _iconView = [UIImageView initWithImageName:@"avatar_default_big"];
        _iconView.layer.cornerRadius = 45;
        _iconView.clipsToBounds = true;
    }
    return _iconView;
}

- (UILabel *)welcomeLabel {
    if (!_welcomeLabel) {
        _welcomeLabel = [UILabel initWithTitle:@"欢迎归来" fontSize:17 color:[UIColor orangeColor]];
    }
    return _welcomeLabel;
}

@end
